package com.ssy.system.controller;

import com.ssy.common.CodeMsg;
import com.ssy.common.Constant;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.pojo.Permission;
import com.ssy.system.service.IPermissionService;
import com.ssy.system.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author sunshuaiyong
 * @ClassName PermissionController
 * @description: TODO
 * @date 2021/10/29 9:06
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;


    /**
     * 加载全部权限
     *
     * @param permissionVo 查询参数
     * @return data
     */
    @GetMapping("/loadAllPermissionByPage")
    public PageResult loadAllPermissionByPage(PermissionVo permissionVo) {
        permissionVo.setType(Constant.TYPE_PERMISSION);
        return this.permissionService.loadAllpermission(permissionVo);
    }


    /**
     * 添加权限
     *
     * @param permission 权限实体
     * @return data
     */
    @PostMapping("/addPermission")
    public Result addPermission(Permission permission) {
        permission.setType(Constant.TYPE_PERMISSION);
        int i = this.permissionService.insertPermission(permission);
        return i > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    /**
     * 修改权限
     *
     * @param permission 权限实体
     * @return data
     */
    @PostMapping("/updatePermission")
    public Result updatePermission(Permission permission) {
        int i = this.permissionService.updatePermissionById(permission);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @DeleteMapping("/deletePermission/{id}")
    public Result deletePermission(@PathVariable Integer id) {
        int i = this.permissionService.deletePermissionById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }


}
